package com.oms.app.user.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oms.app.user.model.Module;
import com.oms.app.user.service.IModuleService;
import com.oms.base.OMSConfigHolder;
import com.oms.page.PageModel;
import com.oms.util.OMSFileUtils;
/**
 * controller
 * @author wtj
 * @date Sep 10, 2012  10:42:41 PM
 */
@RequestMapping("")
public class ModuleAction {
	private IModuleService moduleService;
	private Map<String, Object> map = new HashMap<String, Object>();
	private static final String jspRootPaht = OMSConfigHolder.getWebRoot().replaceAll("\\\\", "/") +  "jsp";
	public void setModuleService(IModuleService moduleService) {
		this.moduleService = moduleService;
	}
	
	/*** 后台管理 BEGIN ***/
	//进入模块管理列表
	@RequestMapping(value="module_moduleManagerList", method=RequestMethod.GET)
	public String enterModuleManagerList(HttpServletRequest request){
		return "manager/module/moduleManagerList";
	}
	// 获取模块组列表
	@ResponseBody
	@RequestMapping(value="/module_getList")
	public Map<String, Object> getModuleList(HttpServletRequest request){
		List list =  moduleService.getModuleList(request);
		return PageModel.getListToString(list, request);
	}
	//进入增加模块
	@RequestMapping(value="module_addOrUpdateModule", method=RequestMethod.GET)
	public void enterAddOrUpdateModule(HttpServletRequest request,HttpServletResponse response){
		
		try {
			PrintWriter out = response.getWriter();
			out.print(OMSFileUtils.readFileToString(jspRootPaht+"/manager/module/addOrUpdateModule.jsp"));
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//return "manager/module/addOrUpdateModule";
	}
	//保存或更新模块
	@ResponseBody
	@RequestMapping(value="/module_saveOrUpdateModule", method=RequestMethod.POST)
	public Map<String, Object> saveOrUpdateModule(Module moduleform, HttpServletRequest request) {
		if(moduleform.getId()!=null&&moduleform.getId().toString().length()>0){// 修改模块
			System.out.println("更新成功！"+moduleService.update(moduleform));
			if(moduleService.update(moduleform)==1){
				map.put("msg", "模块更新成功!");
				map.put("success", true);
			}else{
				map.put("msg", "模块更新失败!");
				map.put("success", false);
			}
		}else{ //增加模块
			if(moduleService.saveOrUpdateModule(moduleform)){
				map.put("msg", "模块增加成功!");
				map.put("success", true);
			}else{
				map.put("msg", "模块增加失败!");
				map.put("success", false);
			}
		}

		return map;
	}
	//删除模块信息 
	@ResponseBody
	@RequestMapping(value="/module_deleteModule/{moduleId}")
	public Map<String, Object> enterShopList(@PathVariable String moduleId, HttpServletRequest request) {
			map.clear();
			if (moduleService.deleteModule(moduleId)) {
				map.put("success", true);
				map.put("msg", "删除模块成功！");
			} else {
				map.put("success", true);
				map.put("msg", "删除模块成功！");
			}
		return map;
	}
	// 修改模块发布状态
	@ResponseBody
	@RequestMapping(value="/module_modifyStatus")
	public Map<String, Object> modifyStatus(HttpServletRequest request) {
		map.clear();
		map.put("moduleid",request.getParameter("moduleId"));
		map.put("status",request.getParameter("status"));
		if (moduleService.modifyStatus(map)) {
			map.put("success", true);
			map.put("msg", "修改模块状态成功！");
		} else {
			map.put("success", true);
			map.put("msg", "修改模块状态失败！");
		}
		return map;
	}
	// 获取 角色 与 模块的关联关系
	@ResponseBody
	@RequestMapping(value="/module_getRoleOfModule/{roleId}")
	public Map<String, Object> getRoleOfModule(@PathVariable String roleId, HttpServletRequest request) {
		map.clear();
		map.put("roleid", roleId);
		List list = moduleService.getRoleOfModule(map);
		return PageModel.getListToString(list, request);
	}
	/*** 后台管理 END ***/
}